package guida.ccf.gzuexaminesystem.controller;

import guida.ccf.gzuexaminesystem.common.HttpStatusDescriptions;
import guida.ccf.gzuexaminesystem.common.ResponseResult;
import guida.ccf.gzuexaminesystem.dto.LoginDto;
import guida.ccf.gzuexaminesystem.service.StudentInfoService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ccf
 * @since 2023-11-03
 */
@Slf4j
@RestController
@RequestMapping("/studentInfo")
@ApiOperation(value = "学生端相关api接口")
public class StudentInfoController {

    @Autowired
    private StudentInfoService studentInfoService;

    /**
     * 学生登录接口
     * @param loginDto 登录实体类: username password jwtId
     * @return 返回登录结果
     */
    @PostMapping("/login")
    @ApiOperation(value = "学生登录接口", notes = "参数是username, password和jwtId")
    public ResponseResult<?> login(@Validated @RequestBody LoginDto loginDto) {
        return studentInfoService.login(loginDto);
    }

    /**
     * 学生退出登录接口
     * @return 返回退出登录结果
     */
    @PostMapping("/logout")
    @PreAuthorize("hasAnyRole('STUDENT')")
    @ApiOperation(value = "学生退出登录接口", notes = "参数无")
    public ResponseResult<?> logout() {
        return studentInfoService.logout();
    }


    /**
     * 测试权限接口
     * @return 测试权限接口
     */
    @PostMapping("/test")
    @ApiOperation("测试权限接口")
    @PreAuthorize("hasAnyRole('STUDENT')")
    public String test() {
        return "具有 ROLE_STUDENT 权限的用户才可以访问该接口!";
    }

    /**
     * 通过学号查姓名
     */
    @GetMapping("/selectName")
    @ApiOperation(value = "通过学号查询姓名", notes = "参数是学号")
    @PreAuthorize("hasAnyRole('STUDENT')")
    public String selectName(@RequestParam("studentId") String studentId) {
        if (!StringUtils.hasLength(studentId)) {
            return "";
        }
        return studentInfoService.selectNameByStudentId(studentId);
    }

}
